package com.service.store.impl;

import com.dao.store.QuestionDao;
import com.domain.store.Question;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.service.store.QuestionService;
import com.utils.MapperUtils;
import org.apache.ibatis.session.SqlSession;
import org.apache.poi.ss.usermodel.HorizontalAlignment;
import org.apache.poi.ss.usermodel.VerticalAlignment;
import org.apache.poi.ss.util.CellRangeAddress;
import org.apache.poi.xssf.usermodel.*;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.Date;
import java.util.List;
import java.util.UUID;

public class QuestionServiceImpl implements QuestionService {
    private QuestionDao questionDao;

    public void setQuestionDao(QuestionDao questionDao) {
        this.questionDao = questionDao;
    }

    /**
     * 查询全部
     * @return
     */
    @Override
    public List<Question> findAll() {
        return questionDao.findAll();
    }

    /**
     * 分页查询
     * @param page
     * @param size
     * @return
     */
    @Override
    public PageInfo findAll(int page, int size) {
            Page pages = PageHelper.startPage(page,size);
            questionDao.findAll();
            return  new PageInfo(pages);

    }

    /**
     * 根据id查询
     * @param id
     * @return
     */
    @Override
    public Question findById(String id) {
       return questionDao.findById(id);
    }

    /**
     * 添加操作
     * @param question
     */
    @Override
    public String save(Question question,boolean flag) {
        String id = UUID.randomUUID().toString();
            question.setId(id);
            //添加当前时间和默认审核
            question.setCreateTime(new Date());
            question.setReviewStatus("0");

            if (flag) {
                //防止图片重名
                question.setPicture(id);
            }
            questionDao.save(question);
        return id;
    }

    /**删除操作
     * @param question
     */
    @Override
    public void delete(Question question) {
        questionDao.delete(question);
    }

    /**
     * 修改操作
     * @param question
     */
    @Override
    public void update(Question question,boolean flag) {
       questionDao.update(question);
    }

    /**
     * 获取报表
     */
    @Override
    public ByteArrayOutputStream toExport() {
        ByteArrayOutputStream bos = null;
        try {
            //获取对应的文件
            XSSFWorkbook workbook = new XSSFWorkbook();
            //创建工作表
            XSSFSheet sheet = workbook.createSheet("数据文件");
            //合并单元格
            sheet.addMergedRegion(new CellRangeAddress(1, 1, 1, 12));

            //制作标题
            XSSFRow row_1 = sheet.createRow(1);
            XSSFCell cell_1_11 = row_1.createCell(1);
            cell_1_11.setCellValue("在线试题导出信息");
            //对齐操作
            //创建样式
            XSSFCellStyle style = workbook.createCellStyle();
            style.setAlignment(HorizontalAlignment.CENTER);//对齐
            style.setVerticalAlignment(VerticalAlignment.CENTER);//垂直对齐
            cell_1_11.setCellStyle(style);

            //表头
            String[] str = {"题目ID","所属公司ID","所属目录ID","题目简介","题干描述","题干配图"
                    ,"题目分析","题目类型","题目难度","是否经典题","题目状态","审核状态"};
            //行
            XSSFRow row_2 = sheet.createRow(2);
            for (int i = 0; i <str.length ; i++) {
                //列
                XSSFCell cell_2_temp = row_2.createCell(1+i);
                cell_2_temp.setCellValue(str[i]);
                //对齐操作
                //创建样式
                XSSFCellStyle style_temp = workbook.createCellStyle();
                style_temp.setAlignment(HorizontalAlignment.CENTER);//对齐
                cell_2_temp.setCellStyle(style_temp);
            }
            List<Question> list  = questionDao.findAll();

            int index = 0;
            for (Question question : list) {
                int data_index = 0;
                XSSFRow row_data = sheet.createRow(3+index++);

                XSSFCell cell_data_1 = row_data.createCell(1+data_index++);
                cell_data_1.setCellValue(question.getId());
                cell_data_1.setCellStyle(style);

                XSSFCell cell_data_2 = row_data.createCell(1+data_index++);
                cell_data_2.setCellValue(question.getCompanyId());
                cell_data_2.setCellStyle(style);

                XSSFCell cell_data_3 = row_data.createCell(1+data_index++);
                cell_data_3.setCellValue(question.getCatalogId());
                cell_data_3.setCellStyle(style);

                XSSFCell cell_data_4 = row_data.createCell(1+data_index++);
                cell_data_4.setCellValue(question.getRemark());
                cell_data_4.setCellStyle(style);

                XSSFCell cell_data_5 = row_data.createCell(1+data_index++);
                cell_data_5.setCellValue(question.getSubject());
                cell_data_5.setCellStyle(style);

                XSSFCell cell_data_6 = row_data.createCell(1+data_index++);
                cell_data_6.setCellValue(question.getPicture());
                cell_data_6.setCellStyle(style);

                XSSFCell cell_data_7 = row_data.createCell(1+data_index++);
                cell_data_7.setCellValue(question.getAnalysis());
                cell_data_7.setCellStyle(style);

                XSSFCell cell_data_8 = row_data.createCell(1+data_index++);
                cell_data_8.setCellValue(question.getType());
                cell_data_8.setCellStyle(style);

                XSSFCell cell_data_9 = row_data.createCell(1+data_index++);
                cell_data_9.setCellValue(question.getDifficulty());
                cell_data_9.setCellStyle(style);

                XSSFCell cell_data_10 = row_data.createCell(1+data_index++);
                cell_data_10.setCellValue(question.getIsClassic());
                cell_data_10.setCellStyle(style);

                XSSFCell cell_data_11 = row_data.createCell(1+data_index++);
                cell_data_11.setCellValue(question.getState());
                cell_data_11.setCellStyle(style);

                XSSFCell cell_data_12 = row_data.createCell(1+data_index++);
                cell_data_12.setCellValue(question.getReviewStatus());
                cell_data_12.setCellStyle(style);
            }


            //创建文件对象,作为报表的内容输出文件
            //通过流对外输出
             bos = new ByteArrayOutputStream();
            //将数据写入流中
            workbook.write(bos);
            //释放资源
            workbook.close();
            MapperUtils.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return bos;
    }
}
